package structural.composite;

/**
 * @ClassName: MainTest
 * @Description: 测试类
 * @Author: liu-hao
 * @Date: 2023-04-09 17:53
 * @Version: 1.0
 **/
public class MainTest {

    public static void main(String[] args) {
        // 维护结构
        Composite root = new Composite();
        Composite subTree1 = new Composite();
        subTree1.add(new Leaf("leaf1"));
        subTree1.add(new Leaf("leaf2"));
        Composite subTree2 = new Composite();
        subTree2.add(new Leaf("leaf3"));
        Composite subTree3 = new Composite();
        subTree3.add(new Leaf("leaf4"));
        subTree3.add(new Leaf("leaf5"));
        subTree2.add(subTree3);
        root.add(subTree1);
        root.add(subTree2);

        // 执行操作
        root.operation();


        tree(root, 0);
    }

    static void tree(Component node, int depth) {
        for(int i=0; i<depth; i++) System.out.print("--");
        node.operation();

        if(node instanceof Composite) {
            for (Component n : ((Composite)node).getChildren()) {
                tree(n, depth + 1);
            }
        }
    }
}
